<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('order.no')" type="string" v-model="baTable.form.items!.no" prop="no" :placeholder="t('Please input field', { field: t('order.no') })" />
                    <FormItem :label="t('order.car_id')" type="remoteSelect" v-model="baTable.form.items!.car_id" prop="car_id" :input-attr="{ pk: 'ba_car.id', field: 'name', 'remote-url': '/admin/Car/index' }" :placeholder="t('Please select field', { field: t('order.car_id') })" />
                    <FormItem :label="t('order.tenant_name')" type="string" v-model="baTable.form.items!.tenant_name" prop="tenant_name" :placeholder="t('Please input field', { field: t('order.tenant_name') })" />
                    <FormItem :label="t('order.tenant_phone')" type="string" v-model="baTable.form.items!.tenant_phone" prop="tenant_phone" :placeholder="t('Please input field', { field: t('order.tenant_phone') })" />
                    <FormItem :label="t('order.tenant_idcard')" type="image" v-model="baTable.form.items!.tenant_idcard" prop="tenant_idcard" />
                    <FormItem :label="t('order.tenant_idcard_back')" type="image" v-model="baTable.form.items!.tenant_idcard_back" prop="tenant_idcard_back" />
                    <FormItem :label="t('order.start_address_detail')" type="string" v-model="baTable.form.items!.start_address_detail" prop="start_address_detail" :placeholder="t('Please input field', { field: t('order.start_address_detail') })" />
                    <FormItem :label="t('order.start_address_lat')" type="string" v-model="baTable.form.items!.start_address_lat" prop="start_address_lat" :placeholder="t('Please input field', { field: t('order.start_address_lat') })" />
                    <FormItem :label="t('order.form_type')" type="select" v-model="baTable.form.items!.form_type" prop="form_type" :data="{ content: { '1': t('order.form_type 1'), '2': t('order.form_type 2'), '3': t('order.form_type 3') } }" :placeholder="t('Please select field', { field: t('order.form_type') })" />
                    <FormItem :label="t('order.person_num')" type="string" v-model="baTable.form.items!.person_num" prop="person_num" :placeholder="t('Please input field', { field: t('order.person_num') })" />
                    <FormItem :label="t('order.destination')" type="string" v-model="baTable.form.items!.destination" prop="destination" :placeholder="t('Please input field', { field: t('order.destination') })" />
                    <FormItem :label="t('order.start_date')" type="datetime" v-model="baTable.form.items!.start_date" prop="start_date" :placeholder="t('Please select field', { field: t('order.start_date') })" />
                    <FormItem :label="t('order.daylen')" type="string" v-model="baTable.form.items!.daylen" prop="daylen" :placeholder="t('Please input field', { field: t('order.daylen') })" />
                    <FormItem :label="t('order.price')" type="number" prop="price" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.price" :placeholder="t('Please input field', { field: t('order.price') })" />
                    <FormItem :label="t('order.amount')" type="number" prop="amount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.amount" :placeholder="t('Please input field', { field: t('order.amount') })" />
                    <FormItem :label="t('order.start_mileage')" type="string" v-model="baTable.form.items!.start_mileage" prop="start_mileage" :placeholder="t('Please input field', { field: t('order.start_mileage') })" />
                    <FormItem :label="t('order.insure_form')" type="select" v-model="baTable.form.items!.insure_form" prop="insure_form" :data="{ content: { '1': t('order.insure_form 1'), '2': t('order.insure_form 2') } }" :placeholder="t('Please select field', { field: t('order.insure_form') })" />
                    <FormItem :label="t('order.start_store_id')" type="remoteSelect" v-model="baTable.form.items!.start_store_id" prop="start_store_id" :input-attr="{ pk: 'ba_store.id', field: 'store_name', 'remote-url': '/admin/Store/index' }" :placeholder="t('Please select field', { field: t('order.start_store_id') })" />
                    <FormItem :label="t('order.start_staff_id')" type="remoteSelect" v-model="baTable.form.items!.start_staff_id" prop="start_staff_id" :input-attr="{ pk: 'ba_staff.id', field: 'staff_name', 'remote-url': '/admin/Staff/index' }" :placeholder="t('Please select field', { field: t('order.start_staff_id') })" />
                    <FormItem :label="t('order.start_petrol')" type="image" v-model="baTable.form.items!.start_petrol" prop="start_petrol" />
                    <FormItem :label="t('order.start_images')" type="image" v-model="baTable.form.items!.start_images" prop="start_images" />
                    <FormItem :label="t('order.remark')" type="textarea" v-model="baTable.form.items!.remark" prop="remark" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('order.remark') })" />
                    <FormItem :label="t('order.complete_date')" type="datetime" v-model="baTable.form.items!.complete_date" prop="complete_date" :placeholder="t('Please select field', { field: t('order.complete_date') })" />
                    <FormItem :label="t('order.complete_address')" type="string" v-model="baTable.form.items!.complete_address" prop="complete_address" :placeholder="t('Please input field', { field: t('order.complete_address') })" />
                    <FormItem :label="t('order.complete_damage_status')" type="select" v-model="baTable.form.items!.complete_damage_status" prop="complete_damage_status" :data="{ content: { '0': t('order.complete_damage_status 0'), '1': t('order.complete_damage_status 1') } }" :placeholder="t('Please select field', { field: t('order.complete_damage_status') })" />
                    <FormItem :label="t('order.complete_damage_images')" type="images" v-model="baTable.form.items!.complete_damage_images" prop="complete_damage_images" />
                    <FormItem :label="t('order.order_status')" type="select" v-model="baTable.form.items!.order_status" prop="order_status" :data="{ content: { '0': t('order.order_status 0'), '1': t('order.order_status 1'), '2': t('order.order_status 2') } }" :placeholder="t('Please select field', { field: t('order.order_status') })" />
                    <FormItem :label="t('order.complete_mileage')" type="string" v-model="baTable.form.items!.complete_mileage" prop="complete_mileage" :placeholder="t('Please input field', { field: t('order.complete_mileage') })" />
                    <FormItem :label="t('order.complete_staff_id')" type="remoteSelect" v-model="baTable.form.items!.complete_staff_id" prop="complete_staff_id" :input-attr="{ pk: 'ba_staff.id', field: 'staff_name', 'remote-url': '/admin/Staff/index' }" :placeholder="t('Please select field', { field: t('order.complete_staff_id') })" />
                    <FormItem :label="t('order.complete_remark')" type="textarea" v-model="baTable.form.items!.complete_remark" prop="complete_remark" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('order.complete_remark') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    start_date: [buildValidatorData({ name: 'date', title: t('order.start_date') })],
    price: [buildValidatorData({ name: 'number', title: t('order.price') })],
    amount: [buildValidatorData({ name: 'number', title: t('order.amount') })],
    complete_date: [buildValidatorData({ name: 'date', title: t('order.complete_date') })],
    create_time: [buildValidatorData({ name: 'date', title: t('order.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('order.update_time') })],
})
</script>

<style scoped lang="scss"></style>
